package com.example.experiment03.Analysisservce.IMPL;


import com.example.experiment03.Analysisservce.CodDataSearchService;
import com.example.experiment03.CODData.Coddata;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class CodDataSearchServiceImpl implements CodDataSearchService {

    @Override
    public List<Coddata> RunKey(String Key) {
        CodDataserviceImpl codDataservice = new CodDataserviceImpl();
        Stream<Coddata> CodModelDataStream = codDataservice.getCodData().parallelStream();
        List<Coddata> CodDataModelList = null;
        if (Key == null) {
            CodDataModelList = CodModelDataStream.sorted((data1, data2) -> -Integer
                    .compare(data1.getLatestTotalCases(), data2.getLatestTotalCases()))
                    .collect(Collectors.toList());
        } else {
            CodDataModelList = CodModelDataStream.filter(data -> data.getState().startsWith(Key) || data.getCountry().startsWith(Key))
                    .sorted((a, b) -> -Integer.compare(a.getLatestTotalCases(), b.getLatestTotalCases()))
                    .collect(Collectors.toList());
        }

        return CodDataModelList;
    }
}

